#!/bin/bash
# Filename:248Deploy
# Author:testdream
# Date:20160112

#脚本使用说明
#1、通过指定命令行的第1个参数以确定部署哪个服务，如部署全部则参数为all.
#all 全部更新
#translator 更新248的媒体转换服务
#restapi
#appstore
#session
#organization
#clientcenter
#schd
#manager  管理后台
#serveplatform
#terminal
#poggyio-apn
#mbox
#safeguard
#social
#poggyio-uc
#domainadmin
#meeting
#extras-ldap

#host变量定义
remoteAddr=xxx.xxx.xxx.25
apiHost=xxx.xxx.xxx.247
managerHost=xxx.xxx.xxx.246
ImHost=xxx.xxx.xxx.250
localhost=xxx.xxx.xxx.248

#路径定义
deployfiles=/xxx/xxx/autoDeploy
remotePath=/xxx/poggyio
remoteManPath=/xxx/xxxx/apache-tomcat-8.0.24/webapps/xxx
targetPath=/xxx/xxx/workplus

managerPath=/xxx/xxx/xxx-3/apache-tomcat-8.0.28/webapps/xxx
manager_back_Path=/xxx/xxx/xxx-3/apache-tomcat-8.0.28

#复制文件夹到本地
if [ -z $2 ];then		
	echo "================================Delete files now!=============================="
	rm -rf $deployfiles/files/*
	echo "================================Copy File Now!================================"
	scp -r -P 22 atwork@$remoteAddr:$remotePath/poggyio-*  $deployfiles/files/

	#复制后台服务
	mkdir $deployfiles/files/manager
	scp -r -P 22 atwork@$remoteAddr:$remoteManPath/*  $deployfiles/files/manager/



	#删除其它文件
	for sinFile in $deployfiles/files/*;do
		if [ -f $sinFile ];then
			rm -f $sinFile
		fi
	done
fi

#执行函数-参数1为部署目标
function deploy248(){
	#echo "===============================BackFiles========================================"
	#rm -rf $targetPath/bakFiles/*$1*
	#deployfiles=/home/atwork/autoDeploy
	echo "=================================Deploy now!===================================="
	i=0
	#覆盖配置文件
	#覆盖conf文件夹
	echo "==============================Update config file!==============================="
	cd $deployfiles/files
	for sinFile in `find ./ -type d  -name  "*$1*"`;do
	echo $sinFile
		if [ -d $sinFile ];then
			cp -rf $deployfiles/conf/$1/* $sinFile			
			i=$[ $i+1 ]
		else
			rm -f $sinFile
		fi
	done
	


	#文件夹是否多于1个
	if [ $i -gt 1 ];then
		echo "=============Oops! The $1 folder more then one!Stop!===================="
		exit 1
	fi
		
		
	#备份删除服务
	echo "==========================Stop service and backup files!=========================="
	#pid_$1=`netstat -ntpl|grep 8083|awk -F[/] '{print $1}'|awk '{print $7}'`
	pid=`jps -v |grep $1|awk '{print $1}'`
	if [ -n "$pid" ];then
		kill -9 $pid
		echo "============The PID of $1 is deleted successfully!====================="
	fi
		
	#删除软链
	#targetPath=/home/atwork/workplus
	cd 	$targetPath
	rm $1
	#备份并更新名称
	i=0
	for sinFold in `find ./ -type d  -name  "*-$1*"`;do
	echo $sinFold
		if [ -d $sinFold ];then
			mv $targetPath/*-$1*  $targetPath/bakFiles/$(basename $sinFold)-$(date +%y%m%d%H%M%S)		
			i=$[ $i+1 ]
		else
			rm -f $sinFold
		fi
	done
	#文件夹是否多于1个
	if [ $i -gt 1 ];then
		echo "=============Oops! The $1 folder more then one!Stop!===================="
	fi
	
	rm -rf $targetPath/*$1*
		
	#复制目标文件夹
	cp -r $deployfiles/files/*$1*  $targetPath/
		
	#启动应用
	echo "==========================Add soft link and start service!========================="
	ln -s $targetPath/*$1*  $targetPath/$1
	
	if [ -n "$pid" ];then
		j=0
		while(($j<60))
		do
			#echo $pid
			port=`netstat -ntpl|grep $pid |awk '{print $1}'`
			if [ -z "$port" ];then
				break
			fi
			sleep 2
			j=$[ $j+1 ]
		done
	fi
	
	sh  $targetPath/$1/bin/start.sh
}


#248服务器部署
echo "=================================Deploy now!===================================="
case $1 in 
	translator|all)
		pid_office=`ps -ef|grep soffice|grep -v grep|awk '{print $2}'`
		if [ -n "$pid_office" ];then
			kill -9 $pid_office
			echo "================The PID of office is deleted successfully!====================="
		fi
		
		deploy248 translator
	;;&

	manager|all)
		if [ -z "$2" ];then
			
			echo "=========================deploy manager service================================="
			echo "================================Delete files now!==============================="
			rm -rf $deployfiles/files/*manager*
			echo "================================Copy File Now!================================="
			scp -rP 3701  atwork@$remoteAddr:$deployfiles/files/*manager*  $deployfiles/files/

			echo "===============================BackFiles========================================"
			rm -rf $manager_back_Path/bakFiles/manager/*
			echo "=================================Deploy now!===================================="
			i=0
			#覆盖配置文件
			echo "==============================Update config file!==============================="
			cd $deployfiles/files
			for sinFile in `find ./ -type d  -name  "*manager*"`;do
			echo $sinFile
				if [ -d $sinFile ];then
					cp -f $deployfiles/conf/manager/application.properties $sinFile/WEB-INF/classes
                    cp -f $deployfiles/conf/manager/quartz.properties $sinFile/WEB-INF/classes
					cp -f $deployfiles/conf/manager/config.json $sinFile/services/jsp/
					i=$[ $i+1 ]
				else
					rm -f $sinFile
				fi
			done
			#文件夹是否多于1个
			if [ $i -gt 1 ];then
				echo "=============Oops! update config!sThe manager folder more then one!Stop!===================="
				exit 1
			fi
			
			
			#备份删除服务
			
			echo "==========================Stop service and delete files!=========================="
			#pid_manager=`netstat -ntpl|grep 8083|awk -F[/] '{print $1}'|awk '{print $7}'`
			pid_manager=`jps -v |grep tomcat|awk '{print $1}'`
			if [ -n "$pid_manager" ];then
				kill -9 $pid_manager
				echo "============The PID of manager is deleted successfully!====================="
			fi
			
			cp -r  $managerPath  $managerPath/../../bakFiles/manager-$(date +%y%m%d%H%M%S)	
			rm -rf $managerPath/*
			
			#复制目标文件夹
			cp -r $deployfiles/files/manager/*  $managerPath/
			
			#启动应用
			echo "==========================start service!========================="
			if [ -n "$pid_manager" ];then
				j=0
				while(($j<60))
				do
					port=`netstat -ntpl|grep $pid_manager |awk '{print $1}'`
					if [ -z "$port" ];then
						
						break
					fi
					sleep 2
					j=$[ $j+1 ]
				done
			fi
		fi
		sh  $managerPath/../../bin/startup.sh


	;;&
esac

exit 0 

